﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using V5_BLL;
using V5_Model;
using V5_Common;

namespace V5_EShop._Template.Default
{
    public partial class ProductChannel : BasicPage
    {
        protected ModelProductClass productclass = new ModelProductClass();
        public int _classid = 0, _showid = 0, _orderid = 0, _pageid = 0;

        protected override void OnInit(EventArgs e)
        {
            this.rpList2.ItemDataBound += new RepeaterItemEventHandler(rpList2_ItemDataBound);
            base.OnInit(e);
        }

        void rpList2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField hf = (HiddenField)e.Item.FindControl("hfProductFields");
            Repeater rp = (Repeater)e.Item.FindControl("rpSingleList");
            if (rp != null)
            {
                DataTable dt = new DataTable();

                dt.Columns.Add(new DataColumn("m1"));
                dt.Columns.Add(new DataColumn("m2"));
                if (hf.Value.Trim().Length > 0)
                {
                    string[] sarr1 = hf.Value.Split(new string[] { "||" }, StringSplitOptions.None);
                    foreach (string s1 in sarr1)
                    {
                        string[] sarr2 = s1.Split(new string[] { ":" }, StringSplitOptions.None);
                        DataRow dr = dt.NewRow();
                        dr["m1"] = sarr2[0];
                        dr["m2"] = sarr2[1];
                        dt.Rows.Add(dr);
                    }
                }
                rp.DataSource = dt;
                rp.DataBind();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                _classid = Input.SetInt(Request["classid"]);
                if (_classid < 1)
                {
                    Response.Redirect("/index.aspx");
                }
                _showid = Input.SetInt(Request["showid"]);
                if (_showid == 0)
                {
                    _showid = 1;
                }
                _orderid = Input.SetInt(Request["orderid"]);
                _pageid = Input.SetInt(Request["pageid"]);
                if (_pageid == 0)
                {
                    _pageid = 1;
                }
                this.Bind_rpList();
                productclass = new BLLProductClass().GetModel(_classid);
            }
        }

        private void Bind_rpList()
        {
            string PageUrl = "/product-channel-[1]-[2]-[3]-{0}." + config.UrlRewrite;
            int count = 0;
            string strWhere = "", strOrder = "ID";
            int IsDesc = 0;
            Pager.CurrentPageIndex = _pageid;
            //开始过滤参数
            if (_classid > 0)
            {
                strWhere += " ClassID=" + _classid;
            }
            else
            {
                strWhere += " ID>0 ";
            }
            //显示类型
            switch (_showid)
            {
                case 1:
                    Pager.PageSize = 12;
                    break;
                case 2:
                    Pager.PageSize = 6;
                    break;
            }
            //销量 价格 排序 默认为1
            switch (_orderid)
            {
                case 1:
                    strOrder = "ID";
                    IsDesc = 1;
                    break;
                case 2://销售量排序
                    strOrder = "ID";
                    IsDesc = 1;
                    break;
                case 3:
                    strOrder = " convert(int,productmemberprice) ";
                    IsDesc = 0;
                    break;
                case 4:
                    strOrder = " convert(int,productmemberprice) ";
                    IsDesc = 1;
                    break;
            }
            string strOrder2 = strOrder;
            if (IsDesc == 0)
            {
                strOrder2 += " desc ";
            }
            else
            {
                strOrder2 += "asc";
            }

            DataTable dt = new BLLProduct().GetList(
                Pager.PageSize,
                Pager.CurrentPageIndex,
                strWhere,
                strOrder,
                IsDesc,
                out count
                ).Tables[0];
            this.Pager.RecordCount = count;

            PageUrl = PageUrl.Replace("[1]", _classid.ToString()).
                            Replace("[2]", _showid.ToString()).
                            Replace("[3]", _orderid.ToString());
            this.Pager.UrlRewritePattern = PageUrl;
            switch (_showid)
            {
                case 1:
                    Bind_rpList1(dt);
                    break;
                case 2:
                    Bind_rpList2(dt);
                    break;
            }
        }

        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            this.Bind_rpList();
        }

        private void Bind_rpList1(DataTable dt)
        {
            this.rpList1.Visible = true;
            this.rpList2.Visible = false;
            this.rpList1.DataSource = dt;
            this.rpList1.DataBind();
        }

        private void Bind_rpList2(DataTable dt)
        {
            this.rpList2.Visible = true;
            this.rpList1.Visible = false;
            this.rpList2.DataSource = dt;
            this.rpList2.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="level"></param>
        /// <param name="loopstr"></param>
        /// <returns></returns>
        public string Get_Level(int level,string loopstr)
        {
            string temp="";
            if (level < 0)
            {
                return temp;
            }
            for (int i = 0; i < level; i++)
            {
                temp += loopstr;
            }
            return temp;
        }
        /// <summary>
        /// 获取品论相关信息
        /// </summary>
        /// <param name="oProductID"></param>
        /// <returns></returns>
        public Comment Get_Comment(object oProductID)
        {
            int iProductID = Input.SetInt(oProductID);
            Comment cm = new Comment();
            //获取等级
            DataTable dt = new BLLProductComment().GetList(" ProductID=" + iProductID).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                cm.Level1 = Input.SetInt(dt.Rows[0]["Pmost"].ToString());
                cm.Level2 = Input.SetInt(dt.Rows[0]["Pmost1"].ToString());
                cm.Level3 = Input.SetInt(dt.Rows[0]["Pmost2"].ToString());
                cm.Level4 = Input.SetInt(dt.Rows[0]["Pmost3"].ToString());
                cm.CountInfo = dt.Rows.Count;
            }
            return cm;
        }

        public class Comment
        {
            /// <summary>
            /// 获取评论等级
            /// </summary>
            public int Level1 { get; set; }
            public int Level2 { get; set; }
            public int Level3 { get; set; }
            public int Level4 { get; set; }
            /// <summary>
            /// 获取评论人数
            /// </summary>
            public int CountInfo { get; set; }
        }
    }
}
